﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class ReadData : MonoBehaviour
{
    public static ReadData instance;
   ///// <summary>
   ///// 大关卡文本
   ///// </summary>
   // public TextAsset leveltxt;
    /// <summary>
    /// 小关卡具体内容文本
    /// </summary>
    private TextAsset levelData, color_shape;
    /// <summary>
    /// 大关卡信息
    /// </summary>
    public Dictionary<int,List<int>> levelDataDic = new Dictionary<int, List<int>>();
    /// <summary>
    /// 小关卡具体内容
    /// </summary>
    public Dictionary<int, MapData> mapDataDic = new Dictionary<int, MapData>();
    /// <summary>
    /// 关卡进度情况
    /// </summary>
    public Dictionary<int, List<int>> loadedData = new Dictionary<int, List<int>>();

    /// <summary>
    /// 需要锁住的关卡
    /// </summary>
    private int[] lockedClose = new int[] {
        1003,1005,2005,3003,4001,4005,5004,5006,6005,7005,8002,8006,9003,9004,10002,10005,11002,11006,12003,12005,13002,13005,14002,14004,15002,15005,16003,16006,17003,18003
    };
    /// <summary>
    /// 保存解锁字典信息
    /// </summary>
    public Dictionary<int, int> UnlockOrNot=new Dictionary<int, int>();
    private void Awake()
    {
        instance = this;
        color_shape = Resources.Load<TextAsset>("level/color_shape");
        levelData= Resources.Load<TextAsset>("level/LevelData1");

        #region  获得关卡对应数组信息
        string[] mapConfigs = levelData.ToString().Split(new string[] { ";\r\n" }, StringSplitOptions.None);
        for (int i = 0; i < mapConfigs.Length; i++)
        {
            if (mapConfigs[i]=="")
            {
                continue;
            }
            string[] map = mapConfigs[i].Split(',');
            MapData mapData = new MapData();
            mapData.big_id = int.Parse(map[0]);
            mapData.id = int.Parse(map[1]);
            Dictionary<string, int> keyValuePairs = new Dictionary<string, int>();
            string[] arr = map[2].Split(new string[] {"\r\n" }, StringSplitOptions.None);
            for (int j=0;j<arr.Length;j++)
            {
                string[] arr1= arr[j].Split('.');
                for(int z=0;z<arr1.Length;z++)
                {
                    if(mapData.mapArray==null)
                    {
                        mapData.mapArray = new string[arr.Length, arr1.Length];
                        mapData.columns = arr1.Length;
                        mapData.rows = arr.Length;
                    }
                    mapData.mapArray[j, z] = arr1[z];
                    if (keyValuePairs.ContainsKey(arr1[z]))
                    {
                        keyValuePairs[arr1[z]]++;
                    }
                    else
                    {
                        keyValuePairs.Add(arr1[z], 1);
                    }
                }
            }
            var sortedAscending = keyValuePairs.OrderBy(kvp => kvp.Key).ToList();
            foreach (var kvp in sortedAscending)
            {
                if (!mapData.mapArrayDic.ContainsKey(kvp.Key))
                {
                    mapData.mapArrayDic.Add(kvp.Key, kvp.Value);
                    mapData.mapArray_list.Add(kvp.Key);
                }
            }
            mapDataDic.Add(mapData.id, mapData);
            ///添加大关卡信息
            if (levelDataDic.ContainsKey(mapData.big_id))
            {
                levelDataDic[mapData.big_id].Add(mapData.id);
            }
            else
            {
                levelDataDic.Add(mapData.big_id, new List<int>());
                levelDataDic[mapData.big_id].Add(mapData.id);
            }
        }
        string[] map_color= color_shape.ToString().Split(new string[] { "|\r\n" }, StringSplitOptions.None);
        for (int i = 0; i < map_color.Length; i++)
        {
            if (map_color[i] == "")
            {
                return;
            }
            string[] str = map_color[i].Split('|');
            for (int j = 0; j < str.Length; j++)
            {
                if(j!=0)
                {
                    if (mapDataDic.ContainsKey(int.Parse(str[0])))
                    {
                        string[] str2 = str[j].Split(',');
                        mapDataDic[int.Parse(str[0])].map_color.Add(str2[0], ((str2[1], str2[2],str2[3]), str2[4]));
                    }
                }
          
            }
        }

        #endregion

        string playerName = "";
        if (PlayerPrefs.HasKey("UnlockOrNot"))
        {
            playerName = PlayerPrefs.GetString("UnlockOrNot");
        }
        else
        {
            for (int i = 0; i < lockedClose.Length; i++)
            {
                if(i!=0)
                {
                    playerName += ",";
                }
                playerName += 0;
            }
        }

        string[] _str = playerName.Split(',');
        for (int i=0;i< lockedClose.Length;i++)
        {
            if (!UnlockOrNot.ContainsKey(lockedClose[i]))
            {
                if(i>=_str.Length)
                {
                    UnlockOrNot.Add(lockedClose[i],0);
                }
                else
                {
                    UnlockOrNot.Add(lockedClose[i], int.Parse(_str[i]));
                }
            }
        }

        loadedData = SlotDataManager.LoadSlotData();
    }

    /// <summary>
    /// 是否观看对应广告
    /// </summary>
    public void SetUnlockOrNot(int id)
    {
        if(UnlockOrNot.ContainsKey(id))
        {
            UnlockOrNot[id] = 1;

            string str = "";
            int num = 0;
            foreach(int key in UnlockOrNot.Keys)
            {
                if(num!=0)
                {
                    str += ",";
                }
                str += UnlockOrNot[key];
                num++;
            }
            PlayerPrefs.SetString("UnlockOrNot", str);
        }
    }


    /// <summary>
    /// 写入数据
    /// </summary>
    public void WriteData(int num)
    {
        if (loadedData.ContainsKey(GameInit.instance.MapData.id))
        {
            loadedData[GameInit.instance.MapData.id][num] = 1;
            SlotDataManager.SaveSlotData(loadedData);
        }

    }

    /// <summary>
    /// 清空当前关卡进度
    /// </summary>
    /// <param name="level"></param>

   public void ClearLoadedData(int level)
    {
        if (loadedData.ContainsKey(level))
        {
            loadedData.Remove(level);
            SlotDataManager.SaveSlotData(loadedData);
        }
    }


    /// <summary>
    /// 获得小关卡内容
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public MapData GetMapById(int id)
    {
        //LevelData level =new LevelData();
        mapDataDic.TryGetValue(id, out MapData l);
        return l;
    }
}
/// <summary>
/// 小关卡地图具体内容
/// </summary>
public class MapData
{
    /// <summary>
    /// 对应的大关卡id
    /// </summary>
    public int big_id;
    /// <summary>
    /// 自身id
    /// </summary>
    public int id;
    /// <summary>
    /// 列
    /// </summary>
    public int columns;
    /// <summary>
    /// 行
    /// </summary>
    public int rows;
    /// <summary>
    /// 具体内容
    /// </summary>
    // public int[,] mapArray;
    public string[,] mapArray;
    /// <summary>
    /// 具体内容字典 (对应id，对应数量)
    /// </summary>
    public Dictionary<string, int> mapArrayDic=new Dictionary<string, int>();
    /// <summary>
    /// 具体内容
    /// </summary>
    public List<string> mapArray_list=new List<string>();
    /// <summary>
    /// 关卡具体颜色以及形状 (颜色，形状)
    /// </summary>
    public Dictionary<string, ((string, string, string), string)> map_color = new Dictionary<string, ((string, string, string), string)>();

}
/// <summary>
/// 大关卡类
/// </summary>
public class LevelData
{
    /// <summary>
    /// 大关卡id
    /// </summary>
    public int levelID;
    /// <summary>
    /// 小关卡id
    /// </summary>
    public List<string> lvId = new List<string>();
}

public enum PenMode
{
    Single,         // 单点
    Tri_Horizontal,  //水平三点 
    Tri_Vertical // 垂直三点
}

